﻿using System;
using System.Collections.Generic;
using System.Linq;
using MaymuseMS.Models;
using MaymuseMS.Models.ValueObject;

namespace MaymuseMS.DAL {
    public class ProductRepository<TEntity> : GenericRepository<TEntity>
        where TEntity : Product, new() {
        UnitOfWork unitOfWork = new UnitOfWork();
        public ProductRepository(MaymuseMSContext context)
            : base(context) { }
        public virtual IEnumerable<AutoCompleteModel> Get(string term) {
            return dbSet.Where(p => p.Name.Contains(term) || p.Number.Contains(term))
                .OrderByDescending(p => p.ProductId).Take(100)
                .Select(p => new AutoCompleteModel {
                    Id = p.ProductId,
                    Name = p.Name,
                    Number = p.Number
                });
        }

        public IEnumerable<AutoCompleteModel> GetPartsSemi(string term) {
            return unitOfWork.PartsRepository.Get(term).Concat(unitOfWork.SemifinishedRepository.Get(term));
        }

        public IEnumerable<AutoCompleteModel> GetFinSemi(string term) {
            return unitOfWork.FinishedRepository.Get(term).Concat(unitOfWork.SemifinishedRepository.Get(term));
        }

        public bool Existence(string number) {
            return dbSet.Where(p => p.Number == number).FirstOrDefault() == null ? false : true;
        }

        public Product GetByNumber(string number) {
            return dbSet.Where(p => p.Number == number).FirstOrDefault();
        }
    }
}